﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Saxon.Api;

namespace WSManager.Logic.Transformation
{
    public class ArticleToXamlTransformer
    {
        XsltTransformer xslt;

        public ArticleToXamlTransformer()
        {
            xslt = XsltTransformerFactory.LoadXslt("Article2Xaml");
        }

        public string ConvertXml(XElement xml)
        {
            if (xml == null) return string.Empty;
            var ser = new Serializer();
            var stream = new MemoryStream();
            ser.SetOutputStream(stream);
            xslt.InitialContextNode = XsltTransformerFactory.BuildDocument(xml.CreateReader());
            xslt.Run(ser);
            stream.Position = 0;
            return new StreamReader(stream).ReadToEnd();
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ConvertXml((XElement)value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
